package org.devocean.groupbuying.web.frontend.displaybeans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.devocean.groupbuying.domain.Article;
import org.devocean.groupbuying.domain.Comment;
import org.devocean.groupbuying.domain.Compatibility;
import org.devocean.groupbuying.domain.Deal;
import org.devocean.groupbuying.domain.Discount;
import org.devocean.groupbuying.domain.Media;
import org.devocean.groupbuying.domain.Merchant;
import org.devocean.groupbuying.domain.Plan;

public class DealBean {

	private Article article;
	// private String mediaFilePath;
	private Discount discount;
	private Deal deal;
	private Map<String, String> additionalContents;
	// private Merchant merchant;
	// private String merchantMediaFilePath;
	private List<Compatibility> compatibilities;
	private Integer stars;

	// private List<Comment> comments;

	public DealBean(Deal deal) {
		this.article = deal.getArticlesLocalization().getArticles().iterator()
				.next();

		// this.stars = (deal.getProduct().getRating()/100)*5;
		this.stars = Math.round((50 / 100) * 5);
		// this.stars = (50/100)*5;
		this.deal = deal;

	}

	public String getMediaFilePath() {
		try {
			return deal.getArticlesLocalization().getMediaLocalizations()
					.iterator().next().getMediaFilePath();
		} catch (Exception e) {
			return "";
		}
	}

	public Deal getDeal() {
		return deal;
	}

	public void setDeal(Deal deal) {
		this.deal = deal;
	}

	public Discount getDiscount() {
		this.discount = deal.getDiscounts().iterator().next();
		for (Discount d : deal.getDiscounts()) {
			if (d.getDiscountPrice() < discount.getDiscountPrice()) {
				discount = d;
			}
		}
		return discount;
	}

	public void setDiscount(Discount discount) {
		this.discount = discount;
	}

	public Map<String, String> getAdditionalContents() {
		return additionalContents;
	}

	public void setAdditionalContents(Map<String, String> additionalContents) {
		this.additionalContents = additionalContents;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}

	public Merchant getMerchant() {
		return deal.getProduct().getMerchant();
	}

	public String getMerchantMediaFilePath() {
		return deal.getProduct().getMerchant().getArticlesLocalization()
				.getMediaLocalizations().iterator().next().getMediaFilePath();
	}
	
	public String getMerchantTitle() {
		return deal.getProduct().getMerchant().getArticlesLocalization()
				.getArticles().iterator().next().getTitle();
	}

	public List<Compatibility> getCompatibilities() {
		this.compatibilities = new ArrayList<Compatibility>();

		for (Plan plan : deal.getProduct().getPlans()) {
			for (Compatibility comp : plan.getCompatibilities()) {
				compatibilities.add(comp);
			}
		}
		return compatibilities;
	}

	public void setCompatibilities(List<Compatibility> compatibilities) {
		this.compatibilities = compatibilities;
	}

	public Integer getStars() {
		return stars;
	}

	public void setStars(Integer stars) {
		this.stars = stars;
	}

	public List<Comment> getComments() {
		return new ArrayList<Comment>(article.getComments());
	}

}
